import { queryMessages, getMsgById, updateMsgReady, getUnReadCount, deleteMsgById, deleteMsgs } from "../../services/workbench/messageCenter";

import { message } from "antd";
const R = require('ramda');
import Constants from '../../utils/Constants';
export default {
    namespace: 'messageCenter',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        messageDetail: {},
        unReadCount: 0
    },
    reducers: {
        /**
         * 设置消息列表
         *
         * @param state
         * @param action
         */
        setMessageList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page }
        },
        /**
         * 设置消息详情
         *
         * @param state
         * @param action
         */
        setMessageDetail(state, { payload }) {
            return { ...state, messageDetail: payload.messageDetail }
        },
        /**
         * 设置消息详情
         *
         * @param state
         * @param action
         */
        setUnReadCount(state, { payload }) {
            return { ...state, unReadCount: payload.unReadCount
             }
        }
    },
    effects: {
        /**
         * 获取消息详情
         *
         * @param call
         * @param put
         * @param select
         */
        *getMsgById({ payload }, { call, put, select }) {
            const res = yield call(getMsgById, { id: payload.id });
            if (res.data.resultCode === "ok") {
                yield put({
                    type: 'setMessageDetail',
                    payload: {
                        messageDetail: R.isNil(res.data.content) ? {} : res.data.content,
                    }
                });
                if(payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 分页查询消息列表
         *
         * @param call
         * @param put
         * @param select
         */
        *queryMessages({ payload }, { call, put, select }) {
            let page = yield select(state => state.messageCenter.page);
            payload.page = R.isNil(payload.page) ? page : payload.page; //首次查询可以不传page
            let status = R.isNil(payload.status) || R.isEmpty(payload.status) ? "1" : payload.status; //status默认是未删除的
            const res = yield call(queryMessages, { isReady: payload.isReady, status: status, type: payload.type, title: payload.title, page: payload.page });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setMessageList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page
                    }
                });
                if(payload.onSuccess) payload.onSuccess(R.isNil(content.list) ? [] : content.list);
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 更新消息为已读
         *
         * @param call
         * @param put
         * @param select
         */
        *updateMsgReady({ payload }, { call, put, select }) {
            const res = yield call(updateMsgReady, { id: payload.id });
            if (res.data.resultCode === "ok") {
                if(payload.onSuccess) payload.onSuccess();
            }else{
                message.error(res.data.errMessage);
            }
        },
        /**
         * 查看未读消息总数
         *
         * @param call
         * @param put
         * @param select
         */
        *getUnReadCount({}, { call, put, select }) {
            const res = yield call(getUnReadCount);
            if (res.data.resultCode === "ok") {
                yield put({
                    type: 'setUnReadCount',
                    payload: {
                        unReadCount: R.isNil(res.data.content) ? 0 : res.data.content,
                    }
                });
            }else {
                message.error(res.data.errMessage);
            }
        },
        /**
         * 批量删除
         *
         * @param call
         * @param put
         * @param select
         */
        *deleteMsgs({payload}, { call, put, select }) {
            const res = yield call(deleteMsgs, {ids: payload.ids});
            if (res.data.resultCode === "ok") {
                message.info("删除成功");
                if(payload.onSuccess)payload.onSuccess();
            }else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 删除消息
         *
         * @param call
         * @param put
         * @param select
         */
        *deleteMsgById({ payload }, { call, put, select }) {
            const res = yield call(deleteMsgById, { id: payload.id });
            if (res.data.resultCode === "ok") {
                message.info("删除成功");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },
    }
};